package com.xxl.job.kettle.core.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.xxl.job.kettle.core.entity.DiScript;
import com.xxl.job.kettle.core.entity.TreeDTO;
import com.xxl.job.kettle.core.service.DiScriptService;
import com.xxl.job.kettle.result.Result;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实现功能：
 *
 * @author 作者[zk]
 * @version 1.0.00  2022-11-29 16:03
 */
//@Api(tags = "资源库脚本管理API")
@RequestMapping("/dataCollection/discript")
@RestController
public class DiScriptController extends BaseController<DiScript, DiScriptService> {

    /**
     * 添加脚本
     *
     * @param param {@link }
     * @return {@link Result}
     */
//    @ApiOperation(value = "添加脚本")
    @PostMapping("/add")
    public Result add(@RequestParam Map<String, String> param) throws Exception {
        return service.add(param);
    }


    /**
     * 删除转换
     *
     * @param id
     * @return
     */
//    @ApiOperation(value = "通过id删除资源库脚本")
    @GetMapping("/delete")
    public Result delete(String repId, String id, String type) throws Exception {
        service.delete(repId, id, type);
        return Result.OK();
    }

    /**
     * 更新资源库
     *
     * @param param {@link DiScript}
     * @return {@link Result}
     */
//    @ApiOperation(value = "更新资源库脚本")
    @PostMapping("/update")
    public Result update(@RequestParam Map<String, String> param) throws Exception {
        // 修改
        service.update(param);
        return Result.OK();
    }

    /**
     * @param id
     * @return
     */
//    @ApiOperation(value = "查询资源库目录下的脚本")
    @GetMapping("/findDiScriptById")
    public Map<String, Object> findDiScriptByCatagoryId(@RequestParam("id") String id,
                                           @RequestParam("repId") String repId,
                                           @RequestParam("page") Integer pageNum,
                                           @RequestParam("limit") Integer limit) {
        Map<String, Object> maps = new HashMap<>();
        if(StrUtil.equals(repId, "0")){
            maps.put("recordsTotal", 0);		// 总记录数
            maps.put("recordsFiltered", 0);	// 过滤后的总记录数
            maps.put("data", new ArrayList<>());
            return maps;
        }
        List<TreeDTO<String>> treeDTOList = service.findDiScriptByCatagoryId(id, repId, pageNum, limit);

        maps.put("recordsTotal", treeDTOList.size());		// 总记录数
        maps.put("recordsFiltered", treeDTOList.size());	// 过滤后的总记录数
        maps.put("data", treeDTOList);
        return maps;
    }

    /**
     * @param jsonObject
     * @return
     */
//    @ApiOperation(value = "移动资源库目录下的脚本到其他目录")
    @PostMapping("/moveScript")
    public Result moveScript(@RequestBody JSONObject jsonObject) throws Exception {
        return service.moveScript(jsonObject);
    }

    /**
     * @param id
     * @return
     */
//    @ApiOperation(value = "查询资源库下指定目录的转换和作业")
    @GetMapping("/findTransAndJobById")
    public Result<Map<String, Integer>> findTransAndJobById(String id) {
        return Result.OK(service.findTransAndJobById(id));
    }
}
